package models

import (
	"activity_srv/basic/config"
	"gorm.io/gorm"
	"time"
)

type CutGoods struct {
	Id          uint32         `gorm:"column:id;type:int UNSIGNED;primaryKey;not null;" json:"id"`
	ActivityId  int32          `gorm:"column:activity_id;type:int;comment:活动id;not null;" json:"activity_id"`                         // 活动id
	GoodsId     int32          `gorm:"column:goods_id;type:int;comment:商品id;not null;" json:"goods_id"`                               // 商品id
	GoodsName   string         `gorm:"column:goods_name;type:varchar(100);comment:商品名称;not null;" json:"goods_name"`                  // 商品名称
	GoodsImg    string         `gorm:"column:goods_img;type:varchar(200);comment:商品图片;not null;" json:"goods_img"`                    // 商品图片
	Stock       int32          `gorm:"column:stock;type:int;comment:库存;not null;" json:"stock"`                                       // 库存
	Desc        string         `gorm:"column:desc;type:varchar(255);comment:商品简介;default:NULL;" json:"desc"`                          // 商品简介
	BasePrice   float64        `gorm:"column:base_price;type:decimal(10, 2);comment:基础价;not null;" json:"base_price"`                 // 基础价
	CutMaxPrice float64        `gorm:"column:cut_max_price;type:decimal(10, 2);comment:用户每次砍价的最大金额;not null;" json:"cut_max_price"`   // 用户每次砍价的最大金额
	CutMinPrice float64        `gorm:"column:cut_min_price;type:decimal(10, 2);comment:用户每次砍价的最小金额;not null;" json:"cut_min_price"`   // 用户每次砍价的最小金额
	CutNum      int32          `gorm:"column:cut_num;type:int;comment:每个用户可砍价的次数;not null;default:1;" json:"cut_num"`                 // 每个用户可砍价的次数
	IsMyCut     int8           `gorm:"column:is_my_cut;type:tinyint(1);comment:是否可以自己砍价 1-否 2是;not null;default:1;" json:"is_my_cut"` // 是否可以自己砍价 1-否 2是
	MinPrice    float64        `gorm:"column:min_price;type:decimal(10, 2);comment:最低价格;not null;" json:"min_price"`                  // 最低价格
	MaxPerson   int32          `gorm:"column:max_person;type:int;comment:最多人数;not null;" json:"max_person"`                           // 最多人数
	MinPerson   int32          `gorm:"column:min_person;type:int;comment:最低人数;not null;" json:"min_person"`                           // 最低人数
	MaxBuy      int32          `gorm:"column:max_buy;type:int;comment:最高购买数;not null;default:1;" json:"max_buy"`                      // 最高购买数
	MinBuy      int32          `gorm:"column:min_buy;type:int;comment:最低购买数;not null;default:1;" json:"min_buy"`                      // 最低购买数
	CreateAt    time.Time      `gorm:"column:create_at;type:datetime;not null;default:CURRENT_TIMESTAMP;" json:"create_at"`
	UpdateAt    time.Time      `gorm:"column:update_at;type:datetime;not null;default:CURRENT_TIMESTAMP;" json:"update_at"`
	DeleteAt    gorm.DeletedAt `gorm:"column:delete_at;type:datetime;default:NULL;" json:"delete_at"`
}

func (c *CutGoods) TableName() string {
	return "cut_goods"
}

// 添加活动商品
func (c *CutGoods) AddCutGoods() error {
	return config.DB.Create(&c).Error
}

// id获取砍价活动商品
func (c *CutGoods) GetById() error {
	return config.DB.Where("id = ?", c.Id).Limit(1).Find(&c).Error
}

// 修改商品
func (c *CutGoods) UpdateCutGoods() error {
	return config.DB.Updates(&c).Error
}

// 商品列表联查活动
func (c *CutGoods) GoodsList() (goods []CutGoodsList, err error) {
	query := config.DB.Model(&CutGoods{}).Select("`cut_goods`.`id`,`cut_goods`.`goods_name`,`cut_goods`.`goods_img`," +
		"`cut_goods`.`stock`,`cut_goods`.`desc`,`cut_goods`.`base_price`,`cut_goods`.`cut_max_price`,`cut_goods`.`max_buy`," +
		"`cut_goods`.`min_buy`,`cut_goods`.`cut_min_price`,`cut_goods`.`cut_num`,`cut_goods`.`min_price`," +
		"`cut_goods`.`max_person`,`cut_goods`.`min_person`,`cut_activity`.`title`," +
		"`cut_activity`.`image`,`cut_activity`.`start_time`,`cut_activity`.`stop_time`").
		Joins("join cut_activity on cut_goods.activity_id = cut_activity.id")
	if c.ActivityId != 0 {
		query = query.Where("activity_id = ?", c.ActivityId)
	}
	err = query.Find(&goods).Error
	return
}

// 商品详情联查活动
func (c *CutGoods) TwoTableDetailGoods() (goods CutGoodsList, err error) {
	err = config.DB.Model(&CutGoods{}).Select("`cut_goods`.`id`,`cut_goods`.`goods_name`,`cut_goods`.`goods_img`,"+
		"`cut_goods`.`stock`,`cut_goods`.`desc`,`cut_goods`.`base_price`,`cut_goods`.`cut_max_price`,`cut_goods`.`max_buy`,"+
		"`cut_goods`.`min_buy`,`cut_goods`.`cut_min_price`,`cut_goods`.`cut_num`,`cut_goods`.`min_price`,"+
		"`cut_goods`.`max_person`,`cut_goods`.`min_person`,`cut_activity`.`title`,"+
		"`cut_activity`.`image`,`cut_activity`.`start_time`,`cut_activity`.`stop_time`").
		Joins("join cut_activity on cut_goods.activity_id = cut_activity.id").Where("cut_goods.id = ?", c.Id).Find(&goods).Error
	return
}
func (c *CutGoods) DelCutGoods() error {
	return config.DB.Delete(&c).Error
}

// 详情
func (c *CutGoods) GoodsDetail() error {
	return config.DB.Where("id = ?", c.Id).Find(&c).Error
}

type CutGoodsList struct {
	Id          uint32    ` json:"id"`
	GoodsName   string    ` json:"goods_name"`
	GoodsImg    string    ` json:"goods_img"`
	Stock       int32     ` json:"stock"`
	Desc        string    ` json:"desc"`
	BasePrice   float64   ` json:"base_price"`
	ActivityId  int32     `json:"activity_id"`
	CutMaxPrice float64   ` json:"cut_max_price"`
	CutMinPrice float64   ` json:"cut_min_price"`
	CutNum      int32     ` json:"cut_num"`
	MinPrice    float64   ` json:"min_price"`
	MaxPerson   int32     ` json:"max_person"`
	MinPerson   int32     ` json:"min_person"`
	Title       string    ` json:"title"`
	Image       string    ` json:"image"`
	MaxBuy      int32     `json:"max_buy"`
	MinBuy      int32     `json:"min_buy"`
	StartTime   time.Time ` json:"start_time"`
	StopTime    time.Time ` json:"stop_time"`
}
